﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Beetle;
using Beetle.FandorinsBugBrain;
using System.Threading;

namespace Console1
{
    class Program
    {
        static void Main(string[] args)
        {
            var beetle = new Bettle("foxiz");

            Beetle.DimaHead.DimaBrain Brain = (Beetle.DimaHead.DimaBrain)beetle.Brain;
            Target[] targets = new Target[]{ new Target(new Position(100, 100), 1)};

            
            var tokenSource2 = new CancellationTokenSource();
            CancellationToken ct = tokenSource2.Token;

            var task = Task.Factory.StartNew(() =>
            {
                while (true)
                {
                    if (ct.IsCancellationRequested)
                    {
                        Brain.neuro.Save("SuperNeurov2.xml");

                        ct.ThrowIfCancellationRequested();
                    }

                    beetle.MakeStepWithLearning(targets);

                    var res = beetle.CaclcError();

                    if (res != -1)
                    {
                        var log = System.IO.File.AppendText(@"c:\logs\errorsv2.txt");
                        log.WriteLine(res);
                        log.Close();

                    }
                }
            }, tokenSource2.Token);

            var task2 = Task.Factory.StartNew(() =>
            {
                while (true)
                {
                    if (ct.IsCancellationRequested)
                    {
                        ct.ThrowIfCancellationRequested();
                    }

                    Brain.neuro.Save("SuperNeurov2.xml");

                    Thread.Sleep(new TimeSpan(0,10,0));
                }
            }, tokenSource2.Token);

            Console.ReadLine();

            tokenSource2.Cancel();

            try
            {
                task.Wait();
                task2.Wait();
            }
            catch (AggregateException e)
            {
            }

            
        }
    }
}
